package com.yaDuo.ticketUnion.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.yaDuo.ticketUnion.R
import com.yaDuo.common.base.BaseVBActivity
import com.yaDuo.common.util.LogUtil
import com.yaDuo.ticketUnion.databinding.ActivityMainBinding
import com.yaDuo.ticketUnion.model.request.NetRepository
import com.yaDuo.ticketUnion.ui.fragment.HomeFragment
import com.yaDuo.ticketUnion.ui.fragment.OnSellFragment
import com.yaDuo.ticketUnion.ui.fragment.SearchFragment
import com.yaDuo.ticketUnion.ui.fragment.SelectedFragment

/**
 * @author YaDuo 2024.11.11
 * 只做显示Fragment和切换导航栏
 * APP的其他逻辑都在Fragment中
 */
class MainActivity : BaseVBActivity<ActivityMainBinding>() {

    companion object {
        const val TAG = "MainActivity"
    }

    private lateinit var mLastFragment: Fragment
    private lateinit var fragmentManager: FragmentManager
    private lateinit var mHomeFragment: HomeFragment
    private lateinit var mOnSellFragment: OnSellFragment
    private lateinit var mSearchFragment: SearchFragment
    private lateinit var mSelectedFragment: SelectedFragment


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initFragments()
        initListener()
        LogUtil.d(TAG, NetRepository.instance.getCategoryList().toString())
    }

    private fun initFragments() {
        mHomeFragment = HomeFragment()
        mOnSellFragment = OnSellFragment()
        mSearchFragment = SearchFragment()
        mSelectedFragment = SelectedFragment()
        mLastFragment = HomeFragment()
        fragmentManager = supportFragmentManager
        switchFragment(mHomeFragment)
    }

    /**
     * NavBar的点击事件
     * TODO：make all Fragments and Activities in MainActivity
     */
    private fun initListener() {
        vb.mainNavigationBar.setOnItemSelectedListener {
            when (it.itemId) {
                // 主页
                R.id.home -> {
                    switchFragment(mHomeFragment)
                }
                // 精选
                R.id.selected -> {
                    switchFragment(mSelectedFragment)
                }
                // 特惠
                R.id.red_packet -> {
                    switchFragment(mOnSellFragment)
                }
                // 搜索
                R.id.search -> {
                    switchFragment(mSearchFragment)
                }
            }
            true
        }
    }

    /**
     * 切换到SearchFragment
     */
    private fun switch2Search() {
        vb.mainNavigationBar.selectedItemId = R.id.search
    }

    /**
     * 切换Fragment
     */
    private fun switchFragment(fragment: Fragment) {
        if (mLastFragment == fragment) {
            LogUtil.d(TAG, "switch same fragment")
            return
        }
        val fragmentTransaction = fragmentManager.beginTransaction()
        if (fragment.isAdded) {
            fragmentTransaction.show(fragment)
        } else {
            fragmentTransaction.add(R.id.main_page_container, fragment)
        }
        fragmentTransaction.hide(mLastFragment)
        mLastFragment = fragment
        fragmentTransaction.commit()

    }
}